package com.five.dao.impl.backstage;

import com.five.dao.backstage.FacTypeDao;
import com.five.entity.backstage.TFacType;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 常鹏宇
 * @Description:
 * @Date: 2020/11/23 22:55
 */
@Repository

public class FacTypeDaoImpl implements FacTypeDao {
    @Override
    public List<Map> queryAll(Integer page, Integer limit, TFacType tFacType) {
        String sql = "select tp.*,count(tf.typeid)count from t_ptype tp left join t_facility tf on tp.id=tf.typeid \n" +
                "  where 1=1 ";
        if (tFacType.getId()!=null){
            sql+=" and tp.id="+tFacType.getId()+" ";
        }
        if (tFacType.getName()!=null){
            sql+=" and tp.name like '%"+tFacType.getName()+"%' ";
        }
        sql+=" group by tp.id limit ?,?";
        List<Map> maps = DBUtil.queryMap(sql,(page-1)*limit,limit);
        return maps;
    }




    @Override
    public int addFacType(TFacType tFacType) {

        if(tFacType.getId() == null){
            String sql = "insert into t_ptype(name) values(?)";
            return DBUtil.manage(sql,tFacType.getName());
        } else{
            String sql = "update t_ptype set name=? where id=?";
            return DBUtil.manage(sql,tFacType.getName(),tFacType.getId());
        }
    }

    @Override
    public int delFacType(TFacType tFacType) {
        String sql = "delete from t_ptype where id = ?";
        return DBUtil.manage(sql,tFacType.getId());
    }

    //获取数据总条数
    @Override
    public int getCount(TFacType tFacType) {
        String sql="select count(*)a from t_ptype tp left join t_facility tf on tp.id=tf.typeid \n" +
                " where 1=1 ";
        if (tFacType.getId()!=null){
            sql+=" and tp.id="+tFacType.getId()+" ";
        }
        if (tFacType.getName()!=null){
            sql+=" and tp.name like '%"+tFacType.getName()+"%' ";
        }
        sql+=" group by tp.id";
        List<Map> maps = DBUtil.queryMap(sql);
        Map map = maps.get(0);
        String s = map.get("a") + "";
        int i = Integer.parseInt(s);
        return i;
    }


    @Override
    public int bad(Integer id) {
        String sql="update t_ptype set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_ptype set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }
}
